package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.dao.BrandMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.service.BrandService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public PageResult findPage(Integer page, Integer size, Map<String, String> searchMap) {
        //条件分页查询
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery();
        if (searchMap.get("name") != null && searchMap.get("name").length() > 0) {
            wrapper.like(Brand::getName, searchMap.get("name"));
        }
        if (searchMap.get("letter") != null) {
            wrapper.eq(Brand::getLetter, searchMap.get("letter"));
        }
        if (searchMap.get("seq") != null) {
            wrapper.eq(Brand::getSeq, searchMap.get("seq"));
        }

        Page<Brand> brandPage = brandMapper.selectPage(new Page<>(page, size), wrapper);
        PageResult<Brand> brandPageResult = new PageResult<>();
        brandPageResult.setTotal(brandPage.getTotal());
        brandPageResult.setRows(brandPage.getRecords());
        return brandPageResult;
    }
}
